package gac.helloworld;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class LocationService extends Service {

	MapHandler map;
	public static Activity ALARMLIST_ACTIVITY;
	private Timer timer = new Timer();
	private static long UPDATE_INTERVAL = 10000; //every minute?
	private String mDestination;
	private int mMonth, mDay, mYear, mHour, mMinute, mWarnTime;
	private boolean alarmSounded = false;
	private Calendar lastChecked = Calendar.getInstance();
	
	@Override
	public void onCreate() {
		super.onCreate();
		map = new MapHandler();
		map.findCurrentLocation(ALARMLIST_ACTIVITY);
		this.startService();
	}
	
	public static void setActivity(Activity a){
		ALARMLIST_ACTIVITY = a;
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		Bundle bundle = intent.getExtras(); 
		if (intent != null && bundle != null)
		{
			mWarnTime = bundle.getInt(AlarmDbAdapter.KEY_WARNTIME);
			mHour = bundle.getInt(AlarmDbAdapter.KEY_HOUR);
			mMinute = bundle.getInt(AlarmDbAdapter.KEY_MINUTE);
			mMonth = bundle.getInt(AlarmDbAdapter.KEY_MONTH);
			mDay = bundle.getInt(AlarmDbAdapter.KEY_DAY);
			mYear = bundle.getInt(AlarmDbAdapter.KEY_YEAR);
			mDestination = bundle.getString(AlarmDbAdapter.KEY_DESTINATION);
			
			map = new MapHandler(ALARMLIST_ACTIVITY, mDestination, mHour, mMinute, mMonth, mDay, mYear, mWarnTime);
		}
	}
	
	 private void startService()
	    {           
	        timer.scheduleAtFixedRate(new mainTask(), 0, UPDATE_INTERVAL);
	        Log.i(getClass().getSimpleName(), "Timer started!!!");
	    }

		public void onDestroy(){
			super.onDestroy();
			this.shutdownService();
		}

		public void shutdownService()
		{
			if (timer != null) timer.cancel();
			Log.i(getClass().getSimpleName(), "Timer stopped...");
			this.stopSelf();
		}
	 
	    private class mainTask extends TimerTask
	    { 
	        public void run() 
	        {
	            getUpdate();
	        }
	    }    
	
	    //called each time the timer ticks
	    private void getUpdate(){
	    	if(!(map.calculateTime() == 0 && map.at00()))
	    	{
	    		if(!alarmSounded)
	    		{
	    			if(map.isTimeToLeave())
	    			{
	    				String ns = Context.NOTIFICATION_SERVICE;
	    				NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
	    				int icon = R.drawable.icon;
	    				CharSequence tickerText = "It's time to leave!";
	    				long when = System.currentTimeMillis();

	    				Notification notification = new Notification(icon, tickerText, when);
	    				notification.defaults |= Notification.DEFAULT_SOUND;
	    				Context context = getApplicationContext();
	    				CharSequence contentTitle = "It's time to leave!";
	    				CharSequence contentText = "";
	    				alarmSounded = true;
	    				Intent i = new Intent(getApplicationContext(), AlarmNotificationActivity.class);
	    				i.putExtra(AlarmDbAdapter.KEY_DESTINATION, mDestination);
	    				PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);

	    				notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
	    				mNotificationManager.notify(1, notification);
	    			}
	    		}
	    		else
	    		{
	    			timer.cancel();
	    		}
	    	}
	    }
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
